package com.xyy.yueshijia.view.store;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.xyy.yueshijia.R;
import com.xyy.yueshijia.base.MvpFragment;
import com.xyy.yueshijia.bean.shop.StoreBannerBean;
import com.xyy.yueshijia.bean.shop.StoreChannelBean;
import com.xyy.yueshijia.bean.shop.StoreQueryBean;
import com.xyy.yueshijia.bean.shop.StoreTag_ClassifyBean;
import com.xyy.yueshijia.presenter.StorePresenter;
import com.xyy.yueshijia.view.activity.SpecialActivity;
import com.xyy.yueshijia.view.impl.StoreView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;


/**
 * Created by Asus on 2017/2/7.
 */

public class StoreFragment
        extends MvpFragment<StorePresenter>
        implements StoreView, SuperRecyclerView.LoadingListener
        ,SuperBaseAdapter.OnItemClickListener {
    //数据
    private List<StoreQueryBean> queryBeanList = new ArrayList<>();
    private List<StoreBannerBean> mBannerBeanList = new ArrayList<>();
    private List<StoreTag_ClassifyBean> mTag_classifyList = new ArrayList<>();
    private StoreChannelBean mChannelBean = new StoreChannelBean();

    @BindView(R.id.store_sr)
    SuperRecyclerView storeSr;
    private StoreQueryAdapter mStoreQueryAdapter;

    @Override
    public StorePresenter initPresenter() {
        return new StorePresenter();
    }

    @Override
    protected void initData() {
        presenter.getModelData(getRequestParams());
    }

    @Override
    protected void initView() {

        storeSr.setLayoutManager(new LinearLayoutManager(mContext));
        storeSr.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//可以自定义下拉刷新的样式
        storeSr.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//可以自定义上拉加载的样式
//        storeSr.setArrowImageView(R.mipmap.iconfont_downgrey);//设置下拉箭头
//        添加适配器
        mStoreQueryAdapter = new StoreQueryAdapter(mContext, queryBeanList);
        storeSr.setAdapter(mStoreQueryAdapter);
//        mStoreQueryAdapter.addHeaderView();

        // 点击 监听
        mStoreQueryAdapter.setOnItemClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.storefragment;
    }

    @Override
    public void onShow() {

    }
    @Override
    public void onFial(Throwable throwable) {
        Log.i("tag","-tttttttttttttttttt-"+throwable.getMessage());
    }

    @Override
    public void onFinish() {

    }

    //
    @Override
    public void onSuccess(List<StoreBannerBean> storeBannerBean, List<StoreTag_ClassifyBean> storeTag_classifyBean, StoreChannelBean storeChannelBean, List<StoreQueryBean> storeQueryBean) {

        queryBeanList.addAll(storeQueryBean);
        mBannerBeanList.addAll(storeBannerBean);
        mTag_classifyList.addAll(storeTag_classifyBean);
        mChannelBean = storeChannelBean;
        mStoreQueryAdapter.notifyDataSetChanged();

    }

    //
    @Override
    public void onStringSuccess(String string) {

    }

    //        http://interface.yueshichina.com/?
// act=goods_cms_special
// &op=cms_special
// &v=2.1.2
// &req_time=1486602902743
// &channel=yingyongbao
// &token=749a036dc06ae8b3a120848995a9f306
// &client=android
// &curpage=1
    public Map<String, String> getRequestParams() {

        Map<String, String> requestParams = new HashMap<>();

        requestParams.put("act", "goods_cms_special");
        requestParams.put("op", "cms_special");
        requestParams.put("v", "2.1.2");
        requestParams.put("req_time", "1486602902743");
        requestParams.put("channel", "yingyongbao");
        requestParams.put("token", "749a036dc06ae8b3a120848995a9f306");
        requestParams.put("client", "android");
        requestParams.put("curpage", "1");

        return requestParams;
    }

    //刷新
    @Override
    public void onRefresh() {
        mStoreQueryAdapter.notifyDataSetChanged();
        storeSr.completeRefresh();

    }

    //    加载更多
    @Override
    public void onLoadMore() {
        mStoreQueryAdapter.notifyDataSetChanged();
        storeSr.completeLoadMore();
    }

    @Override
    public void onItemClick(View view, Object item, int position) {
        // query列表 点击跳转
        String special_id = queryBeanList.get(position).getSpecial_id();
        Intent intent = new Intent(mContext, SpecialActivity.class);
        intent.putExtra("special_id",special_id);

        Log.i("tag","tag------------?"+special_id );
        startActivity(intent);
    }
}
